package data;

import java.util.Date;
import java.util.UUID;

import modele.Manga;

import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;

public class MangaConverter implements Converter {
	
    @SuppressWarnings("rawtypes")
	@Override
    public boolean canConvert(Class clazz) {
        return clazz.equals(Manga.class);
    }
    
    @Override
    public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
        Manga manga = (Manga) value;
        
        // ID
        writer.startNode("id");
        writer.setValue(manga.getId().toString());
        writer.endNode();
        
        // Nom
        writer.startNode("nom");
        writer.setValue(manga.getNom());
        writer.endNode();
        
        // URL
        writer.startNode("url");
        writer.setValue(manga.getUrl());
        writer.endNode();
        
        // No volume
        writer.startNode("no_volume");
        writer.setValue(Integer.toString(manga.no_volume));
        writer.endNode();
        
        // Date de publication
        writer.startNode("date_parution");
        writer.setValue(Long.toString(manga.date_parution.getTime()));
        writer.endNode();
        
        // Nombre de pages
        writer.startNode("nb_pages");
        writer.setValue(Integer.toString(manga.nb_pages));
        writer.endNode();
        
        // Code EAN
        writer.startNode("nom");
        writer.setValue(manga.EAN);
        writer.endNode();
        
        // Nom de l'image
        writer.startNode("image");
        writer.setValue(manga.image.toString());
        writer.endNode();
        
        // Commentaire
        writer.startNode("resume");
        writer.setValue(manga.resume);
        writer.endNode();
    }
    
    @Override
    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        // ID
        reader.moveDown();
        String id = reader.getValue();
        reader.moveUp();
        
        Manga manga = new Manga(UUID.fromString(id));
        
        // Nom
        reader.moveDown();
        manga.setNom(reader.getValue());
        reader.moveUp();
        
        // URL
        reader.moveDown();
        manga.setUrl(reader.getValue());
        reader.moveUp();
        
        // No volume
        reader.moveDown();
        manga.no_volume = Integer.parseInt(reader.getValue());
        reader.moveUp();
        
        // Date de publication
        reader.moveDown();
        manga.date_parution = new Date(Long.parseLong(reader.getValue()));
        reader.moveUp();
        
        // Nombre de pages
        reader.moveDown();
        manga.nb_pages = Integer.parseInt(reader.getValue());
        reader.moveUp();
        
        // Code EAN
        reader.moveDown();
        manga.EAN = reader.getValue();
        reader.moveUp();
        
        // Nom de l'image
        reader.moveDown();
        //manga.image = reader.getValue();;
        reader.moveUp();
        
        // Commentaire
        reader.moveDown();
        manga.resume = reader.getValue();;
        reader.moveUp();
        
        return manga;
    }
}
